package com.moli.honjie.s002test.presenter;

import android.content.Intent;
import android.widget.Toast;

import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.data.BleDevice;
import com.moli.honjie.s002test.callback.BleConnectCallback;
import com.moli.honjie.s002test.ui.ControlActivity;
import com.moli.honjie.s002test.ui.MainActivity;
import com.moli.honjie.s002test.utils.BleUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Administrator on 2018/3/26.
 */

public class MainActivityPresenter {
    private MainActivity mMainActivity;
    private BleDevice mBleDevice;

    public MainActivityPresenter(MainActivity mainActivity) {
        mMainActivity = mainActivity;
    }

    public void startScan() {
        BleUtils.getInstance().scanDevice(mBleScanCallback);
    }

    private BleScanCallback mBleScanCallback = new BleScanCallback() {
        @Override
        public void onScanStarted(boolean success) {
            mMainActivity.showProgressDialog("提示", "蓝牙设备搜索中...");
            mMainActivity.mLeftRecyclerViewAdapter.setNewData(null);
            // mMainActivity.mRightRecyclerViewAdapter.setNewData(null);
        }

        @Override
        public void onScanning(BleDevice result) {
            mMainActivity.mLeftRecyclerViewAdapter.addData(result);
        }

        @Override
        public void onScanFinished(List<BleDevice> scanResultList) {
            // 排序
            if (scanResultList != null && scanResultList.size() > 0) {
                // 去除重复
                List<BleDevice> data = mMainActivity.mRightRecyclerViewAdapter.getData();
                scanResultList.removeAll(data);

                Collections.sort(scanResultList, new Comparator<BleDevice>() {
                    @Override
                    public int compare(BleDevice bleDevice1, BleDevice bleDevice2) {
                        return bleDevice2.getRssi() - bleDevice1.getRssi();
                    }
                });

                mMainActivity.mLeftRecyclerViewAdapter.setNewData(scanResultList);
            }
            mMainActivity.hideProgressDialog();
            Toast.makeText(mMainActivity, "搜索完成", Toast.LENGTH_SHORT).show();
        }
    };


    public void connectDevice(BleDevice item) {
        mBleDevice = item;
        mMainActivity.showProgressDialog("提示", "蓝牙设备连接中...");
        BleUtils.getInstance().connectDevice(item, mBleConnectCallback);
    }

    private BleConnectCallback mBleConnectCallback = new BleConnectCallback() {
        @Override
        public void onConnectSuccess() {
            // 连接成功
            mMainActivity.hideProgressDialog();
            mMainActivity.mLeftRecyclerViewAdapter.getData().remove(mBleDevice);
            mMainActivity.mLeftRecyclerViewAdapter.notifyDataSetChanged();
            mMainActivity.mRightRecyclerViewAdapter.addData(mBleDevice);


            Intent intent = new Intent(mMainActivity,ControlActivity.class);
            mMainActivity.startActivity(intent);

        }

        @Override
        public void onConnectFailure() {
            // 连接失败
            mMainActivity.hideProgressDialog();
            Toast.makeText(mMainActivity, "蓝牙连接失败", Toast.LENGTH_SHORT).show();
        }
    };
}
